package com.aaron.kjviewutils.driving;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.MenuAdapter;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.glide.WebpDataModel;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/3.
 */

public class DrivingMenuAdapter extends  RecyclerView.Adapter<DrivingMenuAdapter.DrivingMenuViewHolder> {

    private Context context;
    private List<DrivingMenuBean> datas;
    private OnItemClickListener mOnItemClickListener;

    public DrivingMenuAdapter(Context context, List<DrivingMenuBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public DrivingMenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.driving_menu_item, parent, false);
        return new DrivingMenuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final DrivingMenuViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty())
        {
            DrivingMenuBean menu = datas.get(position);
            Glide.with(context).load(new WebpDataModel(menu.menuIcon)).placeholder(R.mipmap.ic_launcher).into(holder.itemIcon);
            holder.menuTag.setText((null==menu)?"未知api":(TextUtils.isEmpty(menu.menuName))?"未知api":menu.menuName);
            if(null!=mOnItemClickListener)
            {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }

    public static class DrivingMenuViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.itemIcon)
        ImageView itemIcon;
        @Bind(R.id.menuTag)
        TextView menuTag;

        public DrivingMenuViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}